package com.circle.rest.modular.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.circle.rest.core.aop.Log;
import com.circle.rest.core.aop.Permission;
import com.circle.rest.core.common.enums.BusinessType;
import com.circle.rest.core.common.model.ExcelData;
import com.circle.rest.core.common.model.SuccessResponseData;
import com.circle.rest.core.common.page.PageInfoBT;
import com.circle.rest.core.exception.ServiceException;
import com.circle.rest.core.util.ExcelUtils;
import com.circle.rest.core.util.ToolUtil;
import com.circle.rest.modular.sys.model.OperLog;
import com.circle.rest.modular.sys.service.IOperLogService;
import com.circle.rest.modular.sys.wrapper.OperLogWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 操作日志记录 前端控制器
 * </p>
 *
 * @author 赵航
 * @since 2019-04-02
 */
@Api(value = "操作日志记录", tags = {"sys : 操作日志记录API"})
@RestController
@RequestMapping("/sys/log")
public class OperLogController {

    @Autowired
    private IOperLogService operLogService;

    /**
     * 查询操作日志记录列表
     */
    @ApiOperation("查询操作日志记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index", value = "起始数据下标", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "数据条数", dataType = "int")
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @Log(title = "查询操作日志记录列表", businessType = BusinessType.SELECT_LIST)
    @Permission
    public Object list(Integer index, Integer size) {
        IPage<OperLog> page = new Page<>(index == null ? 1 : index, size == null ? 15 : size);
        QueryWrapper<OperLog> wrapper = new QueryWrapper<>();
        wrapper = wrapper.orderByDesc("oper_time");
        IPage<Map<String, Object>> mapIPage = operLogService.pageMaps(page, wrapper);
        PageInfoBT<Object> objectPageInfoBT = new PageInfoBT<>(new OperLogWrapper(mapIPage).wrap());
        return new SuccessResponseData(objectPageInfoBT);
    }

    /**
     * 查询操作日志记录
     */
    @ApiOperation("查询操作日志记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "操作日志记录编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @Log(title = "查询操作日志记录", businessType = BusinessType.SELECT_ONE)
    @Permission
    public Object detail(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("操作日志记录编号不能为空");
        }
        OperLog operLog = operLogService.getById(id);
        if (ToolUtil.isEmpty(operLog)) {
            throw new ServiceException("操作日志记录不存在");
        }
        return new SuccessResponseData(operLog);
    }

    /**
     * 导出操作日志记录列表
     */
    @ApiOperation("导出操作日志记录列表")
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    @Log(title = "导出操作日志记录列表", businessType = BusinessType.SELECT_LIST)
//    @Permission
    public void export(HttpServletResponse response) throws Exception {
        QueryWrapper<OperLog> wrapper = new QueryWrapper<>();
        wrapper = wrapper.orderByDesc("oper_time");
        List<OperLog> list = operLogService.list(wrapper);

        ExcelData data = new ExcelData();
        List<String> titles = new ArrayList<>();
        titles.add("编号");
        data.setTitles(titles);
        List<List<Object>> rows = new ArrayList<>();
        for (OperLog operLog : list) {
            List<Object> row = new ArrayList<>();
            row.add(operLog.getOperId());
            rows.add(row);
        }
        data.setRows(rows);

        ExcelUtils.exportExcel(response, "操作日志记录" + LocalDateTime.now(), data);
    }
}
